<%
   containerId = 'bubble-chart-widget' + widget.id.to_s
   xMetric = widget_properties["xMetric"]
   yMetric = widget_properties["yMetric"]
   sizeMetric = widget_properties["sizeMetric"]
   xLog = !!widget_properties["xLogarithmic"]
   yLog = !!widget_properties["yLogarithmic"]
   chartHeight = widget_properties["chartHeight"]
   chartTitle = widget_properties["chartTitle"]
%>

<div class="bubble-chart-widget" id="<%= containerId %>">
    <!--[if lte IE 8 ]> <h3><%= message('widget.unsupported_browser_warning') -%></h3> <![endif]-->

    <!--[if (gte IE 9)|!(IE)]><!-->
    <% if chartTitle %>
      <h3><%= h(chartTitle) -%></h3>
    <% end %>
    <!--<![endif]-->
</div>

<!--[if (gte IE 9)|!(IE)]><!-->
<script>
  (function () {
    <%
       filter = MeasureFilter.new
       # Test files should not be returned
       qualifiers = Api::Utils.java_facade.getResourceLeavesQualifiers(@project.qualifier).to_a.reject {|q| q == 'UTS'}
       filter.criteria = {:qualifiers => qualifiers, :base => @project.key}
       filter.metrics = [xMetric.name, yMetric.name, sizeMetric.name]
       filter.execute(self, :user => current_user)
    %>

    var data = {
          metrics: {
            <%= xMetric.name -%>: {
              name: '<%= xMetric.short_name -%>'
            },
            <%= yMetric.name -%>: {
              name: '<%= yMetric.short_name -%>'
            },
            <%= sizeMetric.name -%>: {
              name: '<%= sizeMetric.short_name -%>'
            }
          },
          components: [
            <%
            filter.rows.each  do |row|
              x = row.measure(xMetric)
              y = row.measure(yMetric)
              size = row.measure(sizeMetric)
            %>
            {
              key: '<%= escape_javascript row.resource.key -%>',
              name: '<%= escape_javascript row.resource.name -%>',
              longName: '<%= escape_javascript row.resource.long_name -%>',
              qualifier: '<%= escape_javascript row.resource.qualifier -%>',
              measures: {
                <%= xMetric.name -%>: { val: <%= x ? x.value : 0 -%>, fval: '<%= x ? x.formatted_value : "-" -%>' },
                <%= yMetric.name -%>: { val: <%= y ? y.value : 0 -%>, fval: '<%= y ? y.formatted_value : "-" -%>' },
                <%= sizeMetric.name -%>: { val: <%= size ? size.value : 0 -%>, fval: '<%= size ? size.formatted_value : "-" -%>' }
              }
            },
            <% end %>
          ]
        },
        widget = new SonarWidgets.BubbleChart();

    widget
      .metrics(data.metrics)
      .metricsPriority(['<%= xMetric.name -%>', '<%= yMetric.name -%>', '<%= sizeMetric.name -%>'])
      .components(data.components)
      .height(<%= chartHeight -%>)
      .options({
        baseUrl: baseUrl + '/dashboard/index/',
        xLog: <%= xLog -%>,
        yLog: <%= yLog -%>,
        noData: '<%= message('no_data') -%>',
        noMainMetric: '<%= message('widget.measure_filter.no_main_metric') -%>'
      })
      .render('#<%= containerId -%>');

    autoResize(500, function() {
      widget.update('#<%= containerId -%>');
    });
  })();
</script>
<!--<![endif]-->
